package com.dcits.electronic_account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.dcits.electronic_account.bean.Flow;
import com.dcits.electronic_account.bean.RequestFlow;
import com.dcits.electronic_account.bean.ResponseJson;
import com.dcits.electronic_account.bean.VirtualAccount;
import com.dcits.electronic_account.constant.Constant;
import com.dcits.electronic_account.dto.FlowDto;
import com.dcits.electronic_account.mapper.BankAccountMapper;
import com.dcits.electronic_account.mapper.FlowMapper;
import com.dcits.electronic_account.service.FlowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;



/**
 * @Author: 张志伟
 * @Date： 2022/6/25
 */
@Slf4j
@Service
public class FlowServiceImpl implements FlowService {

    @Autowired
    private FlowMapper flowMapper;

    @Autowired
    private BankAccountMapper bankAccountMapper;

    @Override
    public ResponseJson<List<FlowDto>> queryFlowByList(RequestFlow requestFlow) {
        Integer id = requestFlow.getId();
        if("".equals(id)){
            return new ResponseJson(Constant.FAIL_CODE,"用户信息为空");
        }
        //设置结束开始结束时间
        if(requestFlow.getEndTime() == null){
            //requestFlow.setStartTime(dateToString2(new Date()));
            requestFlow.setEndTime(new Date());
        }
        //设置默认开始时间
        if(requestFlow.getStartTime()==null){
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_WEEK, -30);
            date = calendar.getTime();
            //requestFlow.setEndTime(dateToString2(date));
            requestFlow.setStartTime(date);
        }
        //判断查询类型是否为空
        if(requestFlow.getType() == null){
            requestFlow.setType(3);
        }
        if(requestFlow.getPageNumber() == null){
            requestFlow.setPageNumber(0);
        }else{
            requestFlow.setPageNumber(requestFlow.getPageNumber()*5);
        }


        //根据交易类型，开始时间，结束时间分页查询
        //select fid,time ,type,amount,payer,payee from flow where  payer=1 or payee = 1 and type = 2 and time between 2022-06-25 and 2022-06-25 order  by time asc limit 1,5
        List<Flow> flowsList = flowMapper.selectByTypeAndId(requestFlow.getType(), requestFlow.getStartTime(), requestFlow.getEndTime(), id,requestFlow.getPageNumber());
        ArrayList<FlowDto> arrayList = new ArrayList();
        Integer total = flowMapper.getSize(requestFlow.getType(), requestFlow.getStartTime(), requestFlow.getEndTime(), id,requestFlow.getPageNumber());
        System.out.println(total);
//        Integer total= flowsList.size();
        List<Flow> flows = flowsList;
        //手动分页
//        if(total>5) {
//            if (requestFlow.getPageNumber() * 5 + 5 <= total) {
//                flows = flowsList.subList(requestFlow.getPageNumber() * 5, requestFlow.getPageNumber() * 5 + 5);
//            }
//            else if (requestFlow.getPageNumber() * 5 + 5 > total) {
//                if(requestFlow.getPageNumber() * 5>total){
//                    flows = flowsList.subList(total-5, total);
//                }else {
//                    flows = flowsList.subList(requestFlow.getPageNumber() * 5, total);
//                }
//            }
//
//        }
        //将查询出的数据插入到dto中
        for (Flow flow :flows) {
            flow.setId(id);
            FlowDto flowDto = new FlowDto();
            flowDto.setTime(flow.getTime());
            flowDto.setType(flow.getType());
            flowDto.setAmount(flow.getAmount());
            //根据付款人和收款人id去查询对应的姓名和用户名
            try{
                if (flow.getType() == 1){//提现
                    VirtualAccount payer = flowMapper.queryNameByUserId(flow.getPayer());
                    String s = bankAccountMapper.selectNumber(Integer.valueOf(flow.getPayee()));
                    flowDto.setPayeeUserName(s);
                    flowDto.setPayerUserName(payer.getUserName());
                }else if(flow.getType() ==0){//充值
                    String s = bankAccountMapper.selectNumber(Integer.valueOf(flow.getPayer()));
                    VirtualAccount payee = flowMapper.queryNameByUserId(flow.getPayee());
                    flowDto.setPayerUserName(s);
                    flowDto.setPayeeUserName(payee.getUserName());
                }else{
                    VirtualAccount payee = flowMapper.queryNameByUserId(flow.getPayee());
                    VirtualAccount payer = flowMapper.queryNameByUserId(flow.getPayer());
                    flowDto.setPayeeName(payee.getName());
                    flowDto.setPayeeUserName(payee.getUserName());
                    //存储用户余额到dto
                    flowDto.setBalance(payer.getBalance());
                    //放入dto
                    flowDto.setPayerName(payer.getName());
                    flowDto.setPayerUserName(payer.getUserName());
                }


                flowDto.setTotal(total);
                arrayList.add(flowDto);
            }catch (Exception e){
                e.printStackTrace();
                return  new ResponseJson(Constant.FAIL_CODE,"数据库字段出现问题");
            }
        }
        System.out.println(arrayList);
        return new ResponseJson(Constant.SUCCESS_CODE,arrayList);
    }
}
